﻿package com.utils.slider 
{
	import flash.display.MovieClip;
	import flash.display.Scene;
	import flash.events.MouseEvent;
	
	/**
	 * 滑块部分
	 * @author Kevin
	 */
	public class SliderBar 
	{
		////////////////////////////////////////////////////////////////////////////////
		//
		private var _sliderBar:MovieClip = null;		// 滑块
		//private var _top:MovieClip = null;				// 滑块顶部
		//private var _middle:MovieClip = null;			// 滑块中部
		//private var _bottom:MovieClip = null;			// 滑块底部
		//private var _barLine:MovieClip = null;
		
		//private const LIST:Array = ["top", "bottom", "middle", "barLine"];
		
		////////////////////////////////////////////////////////////////////////////////
		//
		private var _width:Number = 0;					// 滑块最大宽度
		private var _height:Number = 0;					// 滑块最大高度
		private var _x:Number = 0;						// 滑块的水平坐标
		private var _y:Number = 0;						// 滑块的竖直坐标
		
		private var _visible:Boolean = false;
		
		private var _isSelected:Boolean = false;
		
		/**
		 * 可伸缩滑块部分
		 * @param	sliderBar
		 */
		public function SliderBar(sliderBar:MovieClip) 
		{
			_sliderBar = sliderBar;
			_sliderBar.buttonMode = true;
			_sliderBar.mouseChildren = false;
			
			//
			//_top = _sliderBar.top;
			//_middle = _sliderBar.middle;
			//_bottom = _sliderBar.bottom;
			//_barLine = _sliderBar.barLine;
			
			showMouseEffect("out");
		}
		
		/**
		 * 显示滑块
		 */
		public function showBar():void
		{
			_sliderBar.visible = true;
			_visible = true;
			addEvent();
		}
		
		/**
		 * 隐藏滑块
		 */
		public function hideBar():void
		{
			_sliderBar.visible = false;
			_visible = false;
			removeEvent();
		}
		
		/**
		 * 是否选中
		 * @param	isSelect
		 */
		public function select(isSelect:Boolean):void
		{
			_isSelected = isSelect;
			if (_isSelected)
			{
				showMouseEffect("down");
			}
			else
			{
				showMouseEffect("out");
			}
		}
		
		/**
		 * 添加事件侦听
		 */
		private function addEvent():void
		{
			_sliderBar.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			_sliderBar.addEventListener(MouseEvent.ROLL_OUT, outHandler);			
		}
		
		/**
		 * 移除事件侦听
		 */
		private function removeEvent():void
		{
			_sliderBar.removeEventListener(MouseEvent.ROLL_OVER, overHandler);
			_sliderBar.removeEventListener(MouseEvent.ROLL_OUT, outHandler);	
		}
		
		/**
		 * 鼠标移开事件
		 * @param	e
		 */
		private function outHandler(e:MouseEvent):void 
		{		
			if (_isSelected) return;
			showMouseEffect("out");
		}
		
		/**
		 * 鼠标移上事件
		 * @param	e
		 */
		private function overHandler(e:MouseEvent):void 
		{
			if (_isSelected) return;
			showMouseEffect("over");
		}
		
		/**
		 * 跳转到指定效果
		 * @param	frameLabel
		 */
		private function showMouseEffect(frameLabel:String):void
		{				
			//for (var i:int = 0; i < LIST.length; i++)
			//{
				//stopAt(this["_" + LIST[i]], frameLabel);
			//}
			var scene:Scene = _sliderBar.scenes[0];
			var labels:Array = getFrameLabels(scene.labels);
			if (labels.indexOf(frameLabel) != -1)
			{
				_sliderBar.gotoAndStop(frameLabel);
			}
		}
		
		/**
		 * 剪辑是否包含指定帧标签
		 * @param	clip
		 * @param	frameLabel
		 * @return
		 */
		private function stopAt(clip:MovieClip,frameLabel:String):void
		{			
			if (clip == null || clip.totalFrames == 1)
				return;			
			var scene:Scene = clip.scenes[0];
			var labels:Array = getFrameLabels(scene.labels);			
			if (labels.indexOf(frameLabel) == -1) return;			
			clip.gotoAndStop(frameLabel);
		}
		
		/**
		 * 返回帧标签
		 * @param	labels
		 * @return
		 */
		private function getFrameLabels(labels:Array):Array
		{
			var list:Array = [];
			for (var i:int = 0; i < labels.length; i++)
				list[i] = labels[i].name;
			return list;
		}
		
		
		/**
		 * 调整滑块的位置
		 * @param	y
		 */
		private function adjustBar():void
		{
			//_top.y = 0;
			//_middle.y = _top.y + _top.height;
			//if(_barLine)_barLine.y = _middle.y + (_middle.height - _barLine.height) / 2;
			//_bottom.y = _middle.y + _middle.height;
		}
		
		/**
		 * 滑块最大宽度
		 */
		public function get width():Number { return _sliderBar.width; }		
		public function set width(value:Number):void 
		{
			if (value > 10)
			{
				_width = value;
			}
			else
			{
				_width = 10;
			}
			_sliderBar.width = _width;
			adjustBar();
		}
		
		/**
		 * 滑块最大高度
		 */
		//public function get height():Number { return _top.height + _middle.height + _bottom.height }		
		//public function get height():Number { return _sliderBar.height; }		
		//public function set height(value:Number):void 
		//{
			//if (value > _top.height + _bottom.height + 10)
			//{
				//_height = value;
			//}
			//else
			//{
				//_height = _top.height + _bottom.height + 10;
			//}
			//_middle.height = _height - _top.height - _bottom.height;
			//if (value > 10)
			//{
				//_height = value;
			//}
			//else
			//{
				//_height = 10;
			//}
			//_sliderBar.height = _height;
			//adjustBar();
		//}
		
		/**
		 * 滑块的水平坐标
		 */
		public function get x():Number { return _x; }
		public function set x(value:Number):void
		{
			_x = value;
			_sliderBar.x = _x;
		}
		
		///**
		 //* 滑块的竖直坐标
		 //*/
		//public function get y():Number { return _y; }		
		//public function set y(value:Number):void 
		//{
			//_y = value;
			//_sliderBar.y = _y;
		//}
		
		/**
		 * 是否可见
		 */
		public function get visible():Boolean { return _visible; }
		
	}
	
}